package com.hb.trans.mvp.ui.activity


import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.content.ContextCompat
import android.view.MenuItem
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.basiclib.utils.VerifyUtils
import com.basiclib.utils.hideSoftKeyword
import com.basiclib.view.SnackbarUtil
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.hb.trans.R
import com.hb.trans.global.ACTIVITY_SIGN
import com.hb.trans.mvp.contract.SignContract
import com.hb.trans.mvp.presenter.NewDetailPresenter
import com.hb.trans.mvp.presenter.SignPresenter
import com.jakewharton.rxbinding2.view.clicks
import dagger.android.AndroidInjection
import io.reactivex.Observable
import kotlinx.android.synthetic.main.activity_sign.*
import javax.inject.Inject


@Route(path = ACTIVITY_SIGN)
class SignActivity : BaseActivity(), SignContract.View, OnOptionsSelectListener {
    override fun saveError() {
        showShortToast("该身份证号已被注册", SnackbarUtil.ALERT)
    }

    override fun saveSucess() {
        showShortToast("提交成功")
        onBackPressed()
    }


    @Inject
    lateinit var mPresenter: SignPresenter

    @Autowired
    @JvmField
    var busiscope: String = ""

    @Autowired
    @JvmField
    var name: String = ""
    var pvOptions: OptionsPickerView<String>? = null

    lateinit var carTypes: List<String>

//    val carTypes: List<String> by lazy { resources.getStringArray(R.array.car_type).toList() }
    override fun getContentViewResId(): Int = R.layout.activity_sign
    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        mPresenter.attachView(this)
//        ImmersionBar.with(this)
//            .statusBarColor(R.color.colorPrimary)
//            .init()
    }

    private fun showPickView() {
        pvOptions = OptionsPickerBuilder(this, this)
            .setSelectOptions(0)//设置默认选中项
            .setSubmitText("确定")//确定按钮文字
            .setCancelText("取消")//取消按钮文字
            .setSubCalSize(18)//确定和取消文字大小
            .setTitleSize(20)//标题文字大小
            .setSubmitColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))//确定按钮文字颜色
            .setCancelColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))//取消按钮文字颜色
            .setTitleBgColor(ContextCompat.getColor(this, R.color.white))//标题背景颜色 Night mode
            .setBgColor(ContextCompat.getColor(this, R.color.white))//滚轮背景颜色 Night mode
            .setContentTextSize(18)//滚轮文字大小
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .setCyclic(false, false, false)//循环与否
            .setOutSideCancelable(false)//点击外部dismiss default true
            .isDialog(false)//是否显示为对话框样式
            .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
            .build<String>()

        pvOptions!!.setPicker(carTypes, null, null)//添加数据源
        pvOptions!!.show(true)
    }



    override fun onOptionsSelect(options1: Int, options2: Int, options3: Int, v: View?) {



        sign_car.text = carTypes[options1]
    }

    private fun dismissPickerView() {
        if (pvOptions != null && pvOptions!!.isShowing) {
            pvOptions?.dismiss()
        }
    }


    override fun getPresenter(): NewDetailPresenter? = null

    override fun initView() {
        setSupportActionBar(sign_toolbar.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.mipmap.ic_left_arrow)

        sign_school_name.text = name
        sign_school_type.text = "培训类型：" + busiscope
        carTypes = busiscope.split(",")



    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return true
    }

    override fun initData() {
    }

    @SuppressLint("CheckResult")
    override fun setListener() {
//        sign_toolbar.backClick()?.subscribe { finish() }
//
//        sign_toolbar.backClick()?.subscribe {
//            hideSoftKeyword()
//            finish()
//        }
        Observable.mergeArray(sign_name.clicks(), sign_id.clicks(), sign_tel.clicks())
            .subscribe {
                dismissPickerView()
            }


        sign_car.clicks().subscribe {
            hideSoftKeyword()
            showPickView()
        }

//        sign_name.setText("aaa")
//        sign_id.setText("130104199812130321")
//        sign_tel.setText("13900000000")
//        sign_car.setText("A1")

        sign_submit.clicks().subscribe {
            dismissPickerView()
            val phone = sign_tel.text?.toString()?.trim()
            val user_name = sign_name.text?.toString()?.trim()
            val card_no = sign_id.text?.toString()?.trim()
            val car_type = sign_car.text?.toString()?.trim()
            when {
                user_name.isNullOrBlank() -> showShortToast("姓名不能为空")
                card_no.isNullOrBlank() -> showShortToast("身份证号不能为空")
                phone.isNullOrBlank() -> showShortToast("手机号不能为空")
                VerifyUtils.checkPhone(phone) -> showShortToast("手机号码不正确")
                car_type.isNullOrBlank() -> showShortToast("请选择培训车型")
                else -> {
                    mPresenter.save(card_no, phone, user_name, car_type, intent.getStringExtra("inscode"))
                }
            }
        }
    }



}
